<?php

include_once('class-stat.php');

/*
Name: Carbon Paper Vote
Version: 0.1
Author: Bertrand Scarpa
Description: Increase gradually user votes for any posts following goals defined by writer 
Use a distribution profile (Gumbel's law)  
*/
class CVote {
	
	const STEP = 5;
	
	private $ID;
	private $post_ID;
	private $scope;
	private $session;
	private $goal;
	private $score;
	private $gains;
	private $gain;

	public function __construct($ID, $post_ID, $scope, $session, $goal){
		$this->ID = $ID;
		$this->post_ID = $post_ID;
		$this->scope = $scope;
		$this->session = $session;
		$this->goal = $goal;
		$this->set_score();
		$this->set_gains();
		$this->set_gain();
		
	}

	public function get_ID(){
		return $this->ID;
	}

	public function get_post_ID(){
		return $this->post_ID;
	}

	public function get_title(){
		list(,$title)= explode("_reaction_buttons_",$this->ID);
		return $title;
	}

	public function get_goal(){
		return $this->goal;
	}

	public function set_score(){
		global $wpdb;
		$score = $wpdb->get_var("SELECT meta_value from $wpdb->postmeta where post_id =". $this->post_ID . " and meta_key = '{$this->ID}';");
		if(is_null($score)){  // => create a new score line;
			$score = 0;
			$wpdb->insert($wpdb->postmeta, array('post_id' => $this->post_ID, 'meta_key' => $this->ID, 'meta_value' => 0));
		}				
		$this->score = $score;
	}	


	public function get_score(){
		return $this->score;
	}

	public function set_scope($scope){
		$this->scope  = $scope;
	}	


	public function get_scope(){
		return $this->scope;
	}


	public function set_session($session){
		$this->session  = $session;
	}	


	public function get_session(){
		return $this->session;
	}


  	public function set_gains(){
		// Distribute gains by sessions
	    $scale = Cprofiler::$location / log(-log(1/$this->scope));
	    $gains = array_fill(0,$this->scope,0);
	    // Use of the Gumbel inverse probability function to provide distribution :
	    for ($i = 1; $i <= $this->goal; $i++){
	            $u = $i/((int)$this->goal+1);
	            $gumbel_inv = Cprofiler::$location - $scale * log(log(1/$u));
	            $session = round($gumbel_inv);
	            $gains[$session] += 1;
	    }
	
	    $this->gains = $gains;
	}  


	public function get_gains(){
		return $this->gains;
	}

	public function set_gain(){
		// Set gain for the current session
		return $this->gain[$this->session];
	}	

	public function get_gain(){
		return $this->gain;
	}

	public function get_cumuled_gain(){
		return 	array_sum(array_slice($this->gains,0,$this->session));
	}


	public function get_user_gain(){
		$user_gain = $this->score - array_sum(array_slice($this->gains,0,$this->session));
		return ($user_gain >= 0) ? $user_gain : $this->score;
	}


	public function apply_randomized_marge(){
		// Apply randomized marge at +/- $this->r percent from the current session
		$marge = 1-CProfiler::$r/100+(rand(0,10)*CProfiler::$r/500);
		return $marge;
	}	


	public function update(){
		global $wpdb;
		// Update score
		$this->score += $this->gain;
		if ($this->score <= $this->goal) // security: limit generated gains to original goal 
			$wpdb->update($wpdb->postmeta, array('meta_value' => $this->score), array(post_id => $this->post_ID, 'meta_key'  => $this->ID));
	}


	public function render(){
	
		$current_plugins = get_option('active_plugins');
		if (in_array('open-flash-chart-core-wordpress-plugin/open-flash-charts-core.php', $current_plugins)) {
			
			// Pooled Gains 
			$pooled_gains[] = $this->gain[0];
			for ($k = 1; $k < $this->scope; $k+=self::STEP){
				$pooled_gains = array_merge($pooled_gains,array_fill($k, self::STEP-1, null));
				$pooled_gains[] = array_sum(array_slice ($this->gains,$k,self::STEP));
			}

			// Blocked Gains
			$blocked_gains[] = $this->gain[0];
			for ($k = 1; $k < $this->session - fmod($this->session,self::STEP); $k+=self::STEP){
				$blocked_gains = array_merge($blocked_gains,array_fill($k, self::STEP-1, null));
				$blocked_gains[] = array_sum(array_slice($this->gains,$k,self::STEP));
			}
	
			// User Gain
			$user_gain = $this->get_user_gain();
			
			//Chart
			$title = new title("Répartition des gains cumulés du post {$this->get_post_ID()}, vote {$this->get_title()} :\n&nbsp;");
			$title->set_style( "{font-size: 11px; color: #333; font-weight: bold; font-family: 'Lucida Grande',Verdana,Arial,'Bitstream Vera Sans',sans-serif; padding: 7px 7px 8px; text-align: left; line-height: 1.3em;}" );
	
			$x = new y_axis();
			$x->set_range( 0, $this->scope, 10);
			$y = new y_axis();
			$x->colour = '#e3e3e3';
 		   	$x->set_grid_colour('#e2e4ff');
			$y->set_range( 0, max($pooled_gains)*1.1, round(max($pooled_gains)/10));
			$y->colour = '#e3e3e3';
 		   	$y->set_grid_colour('#e2e4ff');
			$bar01 = new bar_filled('#E2D66A','#577261');
			$bar01->set_values( $pooled_gains);
			$bar02 = new bar_filled('#3D5C56','#3D5C56');
			$bar02->set_values( $blocked_gains);
	
			$x_legend = new x_legend("Session {$this->session} / {$this->scope}");
			$x_legend->set_style( "{font-size: 11px; color: #333; font-weight: bold; font-family: 'Lucida Grande',Verdana,Arial,'Bitstream Vera Sans',sans-serif; padding: 7px 7px 8px; text-align: left; line-height: 1.3em;}" );
			$y_legend = new x_legend("Gains");
			$y_legend->set_style( "{font-size: 11px; color: #333; font-weight: bold; font-family: 'Lucida Grande',Verdana,Arial,'Bitstream Vera Sans',sans-serif; padding: 7px 7px 8px; text-align: left; line-height: 1.3em;}" );
			
			$chart = new open_flash_chart();
			$chart->set_bg_colour('#f9f9f9');
			$chart->set_title( $title );
			$chart->set_x_legend( $x_legend );
			$chart->set_y_legend( $y_legend );
			$chart->set_x_axis( $x );
			$chart->set_y_axis( $y );
			$chart->add_element( $bar01 );
			$chart->add_element( $bar02 );
	
			$data=$chart->toPrettyString();
			
			include_once(INCLUDEPATH . '/plugin/vote-profiler/chart.php');
		}
	}
}
?>